<template>
  <div class="app-container">
        <div style="text-align: center;margin-bottom: 50px">
          监控信息{{createStatus?'新增':'更新'}}
        </div>
        <div style="width: 50%;margin:0px auto;vertical-align:middle;height:100%">
        <el-form :model="maintenanceForm" ref="maintenanceForm" :rules="maintenanceFormRules" label-width="150px">

            <el-form-item label="建筑名称" prop="buildId">
              <el-select v-model="maintenanceForm.buildId" placeholder="请选择建筑" style="width: 100%">
                <el-option v-for="(item,index) in buildIdList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="企业名称" prop="enterpriseId" v-if="enterPriseHidden">
              <el-select v-model="maintenanceForm.enterpriseId" placeholder="请选择维保企业" style="width: 100%" >
                <el-option v-for="(item,index) in enterpriseIdList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input type="text" v-model="maintenanceForm.address" placeholder="请输入地址" clearable></el-input>
            </el-form-item>
          <el-form-item label="序列号" prop="serialId">
          <el-input type="text" v-model="maintenanceForm.serialId" placeholder="请输入序列号" clearable></el-input>
          </el-form-item>
          <el-form-item label="公共ip" prop="LANIP">
          <el-input type="text" v-model="maintenanceForm.LANIP" placeholder="请输入ip" clearable></el-input>
          </el-form-item>
          <el-form-item label="载体" prop="PNIP">
          <el-input type="text" v-model="maintenanceForm.PNIP" placeholder="请输入载体名" clearable></el-input>
          </el-form-item>
          <el-form-item label="端口" prop="port">
          <el-input type="text" v-model="maintenanceForm.PNIP" placeholder="请输入端口号" clearable></el-input>
          </el-form-item>
          <div style="text-align: center" class="dialog-footer">
            <el-button @click="cancelMaintenance">取 消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createMaintenance">创 建</el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updateMaintenance">修 改</el-button>
          </div>
        </el-form>
        </div>
  </div>
</template>

<script>
import {getBuildingList} from "@/api/building";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {addInEquipmentMonitor, getInEquipmentMonitorVo, updateInEquipmentMonitor} from "@/api/inEquipmentMonitor";
import {getInMaintenanceRecordVo} from "@/api/maintenanceRecord";
import {getInEquipmentVo} from "@/api/equipment";

export default {
  name: "edit",
  data(){
    return{
      maintenanceForm:{
        intellect:true,
      },
      maintenanceFormRules:{

      },
      addLoading:false,


      buildIdList:[],
      enterpriseIdList:[]

    }
  },
  computed:{
    createStatus(){
      return this.$route.params.ucId =='0'


    },
    enterPriseHidden(){
      let roleCode = this.$store.getters.roleCode;
      return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
    },
  },
  created() {
   this.getBuildIdList()
    this.getEnterpriseIdList()
    let ucId=this.$route.params.ucId
        this.getInEquipmentMonitorVo(ucId)
  },
  methods:{
    cancelMaintenance(){
      window.history.back()
    },
    createMaintenance(){
      this.addLoading = true
      this.$refs['maintenanceForm'].validate(valid => {
        if (valid){
          addInEquipmentMonitor(this.maintenanceForm).then(res=>{
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加监控信息成功"
            })
            this.cancelMaintenance()
          }).catch(res=>{
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        }else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateMaintenance(){
      this.addLoading = true
      this.$refs['maintenanceForm'].validate(valid => {
        if (valid){
          updateInEquipmentMonitor(this.maintenanceForm).then(res=>{
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新信息成功"
            })
            this.cancelMaintenance()
          }).catch(res=>{
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        }else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },


    getInEquipmentMonitorVo(val){
      getInEquipmentMonitorVo({ucId: val}).then(res=>{
        this.maintenanceForm = res.data

      }).catch(error=>{
        console.log(error)
      })

    },
    getBuildIdList(){
      getBuildingList({deleted:false}).then(res=>{
        console.log(res.data)
        this.buildIdList = res.data
      }).catch(error=>{

      })
    },
    getEnterpriseIdList(val){
      getInEnterpriseDominantList({deleted:false}).then(res=>{
        console.log(res.data)
        this.enterpriseIdList = res.data
      }).catch(error=>{

      })
    }
  }
}


</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 149px;
  height: 149px;
  line-height: 149px;
  text-align: center;
}
.avatar {
  width: 149px;
  height: 149px;
  display: block;
}


</style>